package de.s9mtdev.lol_destiny;

import jerklib.ConnectionManager;
import jerklib.Profile;
import jerklib.Session;
import jerklib.events.IRCEvent;
import jerklib.events.IRCEvent.Type;
import jerklib.listeners.IRCEventListener;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;

public class NetworkService extends Service implements IRCEventListener {
	
	private ConnectionManager conManager;
	private Session session;
	private String sessionkey;
	private String sessionpass;
	private String status = "offline";
	
	final Messenger inMessenger = new Messenger(new IncomingHandler());
	private Messenger outMessenger;
	private Messenger outMessenger2;
	
	class IncomingHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			Bundle b = msg.getData();
			if (msg.replyTo != null)
				outMessenger2 = msg.replyTo;
			if (b.getString("session") != null) {
				String s = b.getString("session");
				sessionkey = s.substring(0, 5);
				sessionpass = s.substring(6, 9);
				session.join("#dy_" + sessionkey, sessionpass);								
			}
			else if (b.getString("say") != null)
				session.sayChannel(b.getString("say"), session.getChannel("#dy_" + sessionkey));
			else if (b.getString("exit") != null) {
				session.sayRaw("part #dy_" + sessionkey);
			}
		}
	}

	public NetworkService() {
		super();
		conManager = new ConnectionManager(new Profile("dy_" + Utils.generateRnd(6)));
		session = conManager.requestConnection(Utils.lookUpServer(), 6667);
		session.addIRCEventListener(this);				
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		Bundle extras = intent.getExtras();
	    if (extras != null)
	      outMessenger = (Messenger) extras.get("MESSENGER");
		sendMsg("info", status);
		return inMessenger.getBinder();
	}
	
	public void receiveEvent(IRCEvent e) {
		if (e.getType() == Type.CONNECT_COMPLETE)
		{		
			//session.join("#destiny_lobby");
			status = "online";
			sendMsg("info", status);
		}
		else if (e.getType() == Type.CONNECTION_LOST)
		{		
			status = "offline";
			sendMsg("info", status);
		}
		else if (e.getType() == Type.JOIN_COMPLETE)
		{		
			if (session.getChannel("#dy_" + sessionkey) != null) {
				session.sayRaw("mode #dy_" + sessionkey + " +k " + sessionpass);
			}
		}
		else if (e.getType() == Type.CHANNEL_MESSAGE)
		{
			String data = e.getRawEventData();
			int dataStartsAt = data.lastIndexOf(":") +1;
			data = data.substring(dataStartsAt);
			sendMsg("say", data);
		}
		else if (e.getType() == Type.JOIN)
		{
			if (e.getRawEventData().contains(sessionkey))
				sendMsg("info", "join");
		}
		else if (e.getType() == Type.PART || e.getType() == Type.QUIT)
		{
				sendMsg("info", "part");
		}
		else
			System.out.println(e.getType() + " " + e.getRawEventData());
	}
	
	private void sendMsg(String a, String b) {
		Message msg = Message.obtain();
		try {
			Bundle bundle = new Bundle();
			bundle.putString(a, b);
			msg.setData(bundle);
			if ( outMessenger2 != null ) {				
				outMessenger2.send(msg);
				System.out.println("outMsger2 used");
				}
			outMessenger.send(msg);
		} catch (RemoteException e1) {				
			e1.printStackTrace();
		}
	}
}
